<?php

namespace app\common\model\news;

use think\Model;


class BuyhouseLearn extends Model
{

    // 表名
    protected $name = 'buyhouse_learn';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'display_mode_text',
        'type_text',
        'weigh_text',
        'commentswitch_text',
        'titlecolorlist_text',
        'titleboldswitch_text'
    ];


    protected static function init()
    {
        self::afterInsert(function ($row) {
            self::bindAttachment($row);
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });

        self::afterUpdate(function ($row) {
            self::bindAttachment($row);
        });

        self::afterDelete(function ($row) {
            // 删除图片
            $arr = model("news.BuyhouselearnAttachment")->where('news_id', $row['id'])->select();
            if ($arr) {
                foreach ($arr as $k => $v) {
                    //同步删除oss
                    \think\Hook::listen("upload_delete", $v);
                    $v->delete();
                }
            }
        });
    }

    protected static function bindAttachment($row) {
        $auth = \app\admin\library\Auth::instance();
        // 绑定图片到新闻id
        $_POST = array();
        $_POST['news_id'] = $row['id'];
        $_POST['admin_id'] = $auth->id;
        $newsImage = model('news.BuyhouselearnAttachment');
        if ($row['content']) {
            $pattern = "/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png|\.jpeg|\.bmp]))[\'|\"].*?[\/]?>/";
            preg_match_all($pattern, $row['content'],$match);
            $arr = $match[1];
            if ($arr) {
                // 重设新闻图片
                $newsImage->where('news_id', $_POST['news_id'])->where('is_cover',0)->update(['news_id' => 0]);
                foreach ($arr as $k => $v) {
                    $v = str_replace(\think\Config::get('upload.cdnurl'),'', $v);
                    if ($v) {
                        $newsImage->where('url', $v)->update($_POST);
                    }
                }
            }
        }
        if ($row['image']) {
            $_POST['is_cover'] = 1;
            $newsImage->where('url', $row['image'])->update($_POST);
        }
    }

    public function getDisplayModeList()
    {
        return ['0' => __('Display_mode 0'), '1' => __('Display_mode 1'), '2' => __('Display_mode 2')];
    }

    public function getTypeList()
    {
        return ['0' => __('Type 0'), '1' => __('Type 1'), '2' => __('Type 2'), '3' => __('Type 3')];
    }

    public function getWeighList()
    {
        return ['0' => __('Weigh 0'), '1' => __('Weigh 1'), '2' => __('Weigh 2'), '3' => __('Weigh 3'), '4' => __('Weigh 4'), '5' => __('Weigh 5')];
    }

    public function getCommentswitchList()
    {
        return ['0' => __('Commentswitch 0'), '1' => __('Commentswitch 1')];
    }

    public function getTitlecolorlistList()
    {
        return ['' => __('Titlecolorlist '), 'ff0000' => __('Titlecolorlist ff0000'), '0000ff' => __('Titlecolorlist 0000ff'), '00CD00' => __('Titlecolorlist 00cd00'), '8A2BE2' => __('Titlecolorlist 8a2be2')];
    }

    public function getTitleboldswitchList()
    {
        return ['0' => __('Titleboldswitch 0'), '1' => __('Titleboldswitch 1')];
    }


    public function getDisplayModeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['display_mode']) ? $data['display_mode'] : '');
        $list = $this->getDisplayModeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getWeighTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['weigh']) ? $data['weigh'] : '');
        $list = $this->getWeighList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCommentswitchTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['commentswitch']) ? $data['commentswitch'] : '');
        $list = $this->getCommentswitchList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTitlecolorlistTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['titlecolorlist']) ? $data['titlecolorlist'] : '');
        $list = $this->getTitlecolorlistList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTitleboldswitchTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['titleboldswitch']) ? $data['titleboldswitch'] : '');
        $list = $this->getTitleboldswitchList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function admin()
    {
        return $this->belongsTo('app\admin\model\Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function category()
    {
        return $this->belongsTo('app\common\model\Category', 'category_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
